AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:KeyValue(k,v)
  self[k] = v
end 

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self.Entity:GetPhysicsObject()
	
	if WireAddon ~= nil then
		self.Inputs = Wire_CreateInputs(self.Entity, { "On" })
		self.Outputs = Wire_CreateOutputs(self.Entity, { "On" })
	end
	
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	self.ID = math.random(1000);
	self.BeamTarget = SpawnTarget(self.Entity, self.length, self.pitch, self.ID)
	self.BeamEnt = SpawnBeam(self.Entity, self.width, self.damage, self.material, self.spawnflags, self.offset, self.pitch, self.BeamTarget, self.ID)
	if ( self.starton ) then self.BeamEnt:Fire("turnon","",0) else self.BeamEnt:Fire("turnoff","",0) end
end

function ENT:Think()
	self.BaseClass.Think(self)
	
	if self.Entity:GetTable():GetOn() then
		self.BeamEnt:Fire("turnon","",0)
		if WireAddon ~= nil then Wire_TriggerOutput(self.Entity, "On", 1) end
	else
		self.BeamEnt:Fire("turnoff","",0)
		if WireAddon ~= nil then Wire_TriggerOutput(self.Entity, "On", 0) end
	end
	
	self.Entity:NextThink(CurTime()+0.04)
	return true
end	

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		local etab = self.Entity:GetTable()
		if (value == 0) then etab:SetOn( false )
		else etab:SetOn( true )	end
	end
end

local function Laser_On( pl, ent )
	if ( !ent || ent == NULL ) then return end
	
	local etab = ent:GetTable()
	etab:SetOn( !etab:GetOn() )
end

local function Laser_Off( pl, ent )

	if ( !ent || ent == NULL ) then return end
	local etab = ent:GetTable()
	
	if ( etab:GetToggle() ) then return end
	
	etab:SetOn( !etab:GetOn() )

end

numpad.Register( "Laser_On", Laser_On )
numpad.Register( "Laser_Off", Laser_Off )

function SpawnTarget( ent, length, pitch, id )
    BeamTarget = ents.Create("info_target")
    BeamTarget:SetKeyValue("targetname", "laser_"..id)
	BeamTarget:SetPos(ent:GetPos() + Vector( 0, 0, tonumber(length) ))
	if ( tonumber(pitch) == 90 ) then
		BeamTarget:SetPos(ent:GetPos() + Vector( 0, 0, tonumber(length) ))
	elseif ( tonumber(pitch) == 270 ) then
		BeamTarget:SetPos(ent:GetPos() + Vector( 0, 0, -tonumber(length) ))
	elseif ( tonumber(pitch) == 180 ) then
		BeamTarget:SetPos(ent:GetPos() + Vector( -tonumber(length), 0, 0 ))
	else
		BeamTarget:SetPos(ent:GetPos() + Vector( tonumber(length), 0, 0 ))
	end
    BeamTarget:Spawn()
	BeamTarget:SetParent(ent)
	
	return BeamTarget
end

function SpawnBeam( ent, width, damage, material, spawnflags, offset, pitch, target, id )
    Beam = ents.Create("env_laser")
	if ( tonumber(pitch) == 90 ) then
		Beam:SetPos(ent:GetPos() + Vector( 0, 0, tonumber(offset) ))
	elseif ( tonumber(pitch) == 270 ) then
		Beam:SetPos(ent:GetPos() + Vector( 0, 0, -tonumber(offset) ))
	elseif ( tonumber(pitch) == 180 ) then
		Beam:SetPos(ent:GetPos() + Vector( -tonumber(offset), 0, 0 ))
	else
		Beam:SetPos(ent:GetPos() + Vector( tonumber(offset), 0, 0 ))
	end
    Beam:SetKeyValue("renderamt", "255")
    Beam:SetKeyValue("rendercolor", "255 255 255")
    Beam:SetKeyValue("texture", material..".vmt")
    Beam:SetKeyValue("TextureScroll", "20")
    --Beam:SetKeyValue("targetname", "laser_"..Beam)
    Beam:SetKeyValue("damage", damage)
    Beam:SetKeyValue("spawnflags", spawnflags)
    Beam:SetKeyValue("width", width )
    Beam:SetKeyValue("dissolvetype", "1")
    Beam:SetKeyValue("LaserTarget", "laser_"..id)
    Beam:Spawn()
	Beam:SetParent(ent)
	
	return Beam
end
